﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using Model;
using DAL;

namespace Pay1
{
    public partial class Add : Form
    {
        string IDVALUE = Form1.IDValue;
        int Line = 0;   //明细行数
        DataTable DT = new DataTable();
        SqlDataAdapter SDA = new SqlDataAdapter();

        #region 基本操作

        public Add()
        {
            InitializeComponent();
            NumInit();
            ComboInit();
            if (IDVALUE != null)
            {
                this.Text = "编辑数据";
                Line = InitParameters();
            }
            else    GridViewBind();
        }
        
        public int InitParameters()
        {
            Model.PaySpend ps = DAL.PaySpend.GetModel(Convert.ToInt32(IDVALUE));
            label11.Text = ps.编号;
            comboBox1.Text = ps.类型; 
            dateTimePicker1.Value = Convert.ToDateTime(ps.日期);
            comboBox2.Text = ps.收支;
            txtMoney.Text = Convert.ToString(ps.金额);
            txtCoder.Text = ps.操作员;
            txtMarks.Text = ps.备注;
            DT = GridViewBind();
            return DT.Rows.Count;
        }

        public DataTable GridViewBind()
        {
            SqlCommand cmd = new SqlCommand(string.Format("select 序号, 编号, 明细, 金额, 备注 from PayDetail where 编号 like '{0}'", label11.Text), DAL.DBHelper.Connection);
            SDA.SelectCommand = cmd;
            SDA.Fill(DT);
//             DataTable dtDetail = DAL.PayDetail.GetDetailList(label11.Text);
            this.dataGridView1.DataSource = DT;
            this.dataGridView1.Columns["编号"].Visible = false;
            this.dataGridView1.Columns["序号"].Visible = false;
            return DT;
        }

        public void ComboInit()
        {
            comboBox1.Text = "日常开销";
            comboBox2.Text = "杨潇贾奇杰";
            comboBox3.Text = "支出";
            txtCoder.Text = "贾奇杰";
            txtMoney.Text = "0.00";
        }

        public string NumInit()   //编号初始化
        {
            int i = 1;
            string str = "";
            string strnow = DateTime.Now.ToString("yyyyMMdd");
            DataTable dt = DAL.PaySpend.GetSpendList();
            if (dt.Rows.Count != 0)
            {
                string strnum = dt.Rows[dt.Rows.Count - 1][0].ToString().Substring(10, 3);  //取号码
                string strdate = dt.Rows[dt.Rows.Count - 1][0].ToString().Substring(2, 8);  //取日期
                if (strdate == strnow)
                {
                    i += Convert.ToInt32(strnum);
                    str = string.Format("{0:D3}", i);
                    label11.Text = strnow + str;
                }
                else
                {
                    str = string.Format("{0:D3}", i);
                    label11.Text = strnow + str;
                }
            }
            else
            {
                str = string.Format("{0:D3}", i);
                label11.Text = strnow + str;
            }
            return label11.Text;
        }

        public Model.PaySpend GetSpendModel()
        {
            Model.PaySpend payspend = new Model.PaySpend();
            payspend.编号 = (IDVALUE != null) ? label11.Text : ("DH"+label11.Text);
            payspend.类型 = comboBox1.GetItemText(comboBox1.SelectedItem);
            payspend.日期 = Convert.ToDateTime(dateTimePicker1.Value.ToString("yyyy-MM-dd"));
            payspend.相关人 = comboBox2.GetItemText(comboBox2.SelectedItem);
            payspend.操作员 = txtCoder.Text;
            payspend.备注 = txtMarks.Text;
            payspend.金额 = (comboBox3.Text == "支出") ? -System.Math.Abs(Convert.ToDecimal(txtMoney.Text)) : System.Math.Abs(Convert.ToDecimal(txtMoney.Text));
            payspend.收支 = comboBox3.Text;
            payspend.序号 = Convert.ToInt32(IDVALUE);
            return payspend;
        }
        public Model.PayDetail GetDetailModel(int rIndex)
        {
            Model.PayDetail paydetail = new Model.PayDetail();
            paydetail.编号 = GetSpendModel().编号;
            paydetail.明细 = (this.dataGridView1.Rows[rIndex].Cells["明细"].Value == null) ? "" : this.dataGridView1.Rows[rIndex].Cells["明细"].Value.ToString();
            paydetail.金额 = Convert.ToDecimal(this.dataGridView1.Rows[rIndex].Cells["金额"].Value);
            paydetail.备注 = (this.dataGridView1.Rows[rIndex].Cells["备注"].Value == null) ? "" : this.dataGridView1.Rows[rIndex].Cells["备注"].Value.ToString();
            DataTable dt = DAL.PayDetail.GetList("");
            if (IDVALUE != null && dt.Rows.Count!=0 && dataGridView1.Rows.Count-1 ==Line)
            {
                paydetail.序号 = Convert.ToInt32(dt.Rows[rIndex][0]);
            }
            return paydetail;
        }

//         public int AddDetail(int linecount, int insertline)//para1-明细行数  para2-起始行
//         {
//             int d = 0;
//             for (int i = 0; i < linecount - insertline; i++)
//             {
//                 Model.PayDetail pd = GetDetailModel(insertline + i);
//                 d += DAL.PayDetail.Add(pd);
//             }
//             return d;
//         }

        #endregion

        private void btnSave_Click(object sender, EventArgs e)
        {
            Model.PaySpend ps = GetSpendModel(); 
            for (int i = 0; i < DT.Rows.Count; i++)   DT.Rows[i]["编号"] = ps.编号;
            SqlCommandBuilder SCB = new SqlCommandBuilder(SDA);
            SDA.Update(DT);//更新明细

            if (IDVALUE != null && MessageBox.Show("确实要修改吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)  //更新
            {
                bool state = DAL.PaySpend.UpdateSpend(ps);
                if (state)  MessageBox.Show("记录修改已成功OK");
            } 
            else
            {
                int s = DAL.PaySpend.Add(ps); //条目 
                if (s>0) MessageBox.Show("记录添加已成功OK");
            }
            this.Close();
            this.DialogResult = DialogResult.OK;            
        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) //序号列自增1
        {
            foreach (DataGridViewRow dr in dataGridView1.Rows)
            {
                dr.Cells[0].Value = dr.Index + 1;
            }
        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.Columns[e.ColumnIndex].Name == "金额" && dataGridView1.CurrentCell.Value.ToString() != "" && dataGridView1.Rows[e.RowIndex].Cells["金额"].Value.ToString() != "")
            {
                double sum = 0;
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    sum += Convert.ToDouble(dataGridView1.Rows[i].Cells["金额"].Value);
                }
                txtMoney.Text = sum.ToString("0.00");                  
            }
        }
    }
}
